package com.xuanwei.core.validate;

import com.xuanwei.core.annos.Contain;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author jiangyong
 * @Description  校验是否包含数组
 * @Date 2019/8/15 10:59
 **/
public class ContainValidate implements ConstraintValidator<Contain, Integer> {

    private int[] values;

    @Override
    public void initialize(Contain constraintAnnotation) {
        this.values = constraintAnnotation.values();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (value != null) {
            for (int v : values) {
                if (v == value) {
                    return true;
                }
            }
        }
        return false;
    }
}
